var gTO = 0; // TimeOut
var gCF = 0; // ChekForm
var gLI; //gnLookUpIndex
var gLM; //gnLookUpMax
var gLV; //gcLookUpValue
var gLF; //gcLookUpField
var gRI = './imagenes';//gcRootImage 

function setMessage( cMe , nNi )
{
	oEl = newDiv( 'mensajes' ); 
	oEl.style.zIndex = 99;
	if ( cMe )
	{
		// Setea el estilo del Mensaje de acuerdo al nivel
		var clases = Array ( 'mensaje_error', 'mensaje_advertencia', 'mensaje_aviso');
		// Setea el Mensaje
		oEl.innerHTML = "<div style='padding: 3px;' class='" + clases[nNi] + "' ><div style='float:right;width: 10px'><a onclick='showHideElement(\"mensajes\");'>x</a></div>" + cMe + "</div>";
	} else oEl.innerHTML = "";
}
function hideContextHelp( oIn )
{
	setMessage( '', 2);
	return 1;
}
function showContextHelp( oIn )
{
	setMessage( oIn.title, 2);
	return 1;
}
//////////////////////////////// InputFile ////////////////////////////////
function refreshFile()
{
	setSending ( 0 );
	if (goXHR.readyState==4)
	{
		oEl = id( gcElement );
		oTxt = id("txt" + gcElement);
		oTxt.innerHTML = goXHR.responseText;
		oEl.style.visibility = "visible";
		//gcElement = '';
	}
}
function sendFile( cFN, cUrl, cFi )
{
	frm = id(cFN);
	showHideElement( cFi + '_text', 'show');
	// Toma los datos del Form original
	cAction= frm.action;
	cPost = frm.method;
	cEncType = frm.enctype;
	cTarget = frm.target;
	// Cambia los datos del Form
	frm.action = cUrl;
	frm.method = 'post';
	frm.enctype = 'multipart/form-data';
	frm.target = 'iframe' + cFi ;
	frm.submit();
	// Vuelve a setear los datos del Form original
	frm.action = cAction;
	frm.method = cPost;
	frm.enctype = cEncType;
	frm.target = cTarget;
	return 0;
}
//////////////////////////////// InputDate ////////////////////////////////
var gFe;
function createCalendar( cFi, cFH, cEA, cEH, cEM, cEP  )
{
	oFe = new Date();
	gFe = null;
	oFi = id( cFi );
	if ( oFi.value.length >= 3 ) 
	{
		aFecha = oFi.value.split('/');
		if ( aFecha.length >= 2 && aFecha.length <= 3 )  
		{
			if ( !isNaN(aFecha[0]) ) oFe.setDate(aFecha[0]);
			if ( !isNaN(aFecha[1]) ) oFe.setMonth(aFecha[1]-1);
			if ( aFecha.length == 3 && !isNaN(aFecha[2]) )  oFe.setYear(aFecha[2]);
		}
	}
	htm= '<table style="width: 100%"><thead><tr id="'+ cFi +'_rowhead"><th><a href="javascript://void(0);" onclick="javascript:refreshCalendar(\'' + cFi +'\', ' + (cFH == null ? 'null': '\'' + cFH + '\'') + ', -1, \'' + cEA +'\', \'' + cEH +'\', \'' + cEM +'\', \'' + cEP +'\' );"><<</a></th><th style="display: none; width: 0px;">' + (oFe.getMonth() +1) +'</th><th colspan="3"></th><th>' + oFe.getFullYear()+'</th><th><a href="javascript://void(0);" onclick="javascript:refreshCalendar(\'' + cFi +'\',' + (cFH == null ? 'null': '\'' + cFH + '\'') +', 1, \'' + cEA +'\', \'' + cEH +'\', \'' + cEM +'\', \'' + cEP +'\' );">>></a></th></tr></thead><tbody style="text-align: center" id="'+ cFi +'_body"><tr><td>Lun</td><td>Mar</td><td>Mie</td><td>Jue</td><td>Vie</td><td>Sab</td><td>Dom</td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table>';
	createWindow( cFi + '_divCalendar', cFH == null ? 'Seleccione una fecha' : 'Seleccione dos fechas', htm , 200, 180, null, null ) ;
	refreshCalendar( cFi, cFH, 0 , cEA, cEH, cEM, cEP );
}
function selectDate( cFi , val )
{
	oFi = id( cFi  );
	oFi.value = val;
	destroyWindow( cFi + "_divCalendar");
}
function selectDatePeriod( cFi , cVa , cFH )
{
	if ( gFe == null )  
	{
		gFe = cVa;
		return 1;
	}
	oFi = id( cFi  );	
	oFH = id( cFH );	
	oFi.value = gFe;
	oFH.value = cVa;
	destroyWindow( cFi + "_divCalendar");	
}
function refreshCalendar( cFi, cFH, nOffset, cEA, cEH, cEM, cEP  )
{
	var mes = Array ('','Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
	var max = Array("", 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
	var row = id( cFi + '_rowhead' );
	var m = parseInt( row.cells[1].innerHTML);
	var y = parseInt( row.cells[3].innerHTML);
	m += nOffset;
	if ( m  < 1 ) { m = 12; y--; }
	if ( m > 12 ) { m = 1; y++; }
	row.cells[1].innerHTML = m;
	row.cells[2].innerHTML  = mes[m];
	row.cells[3].innerHTML = y;
	var oFe = new Date(y,(m-1), 1);
	var oHoy = new Date();
	oHoy.setHours(0,0,0,0);
	day  = oFe.getDay() ;
	if ( day == 0 )  day = 7;
	tab = id( cFi + '_body' );
	wk = Math.ceil ( (day - 1 + max[m] )/ 7 );
	tab.rows[5].style.visibility = (wk>=5 ) ?"visible": "hidden";
	tab.rows[6].style.visibility = (wk>=6 ) ?"visible": "hidden";
	d =  2 - day;	
	fun = cFH ==null ? 'selectDate':'selectDatePeriod';
	for(i = 1; i <= wk; i++ )
		for( j= 0; j < 7; j++, d++ )
			if ( d >= 1 && d <= max[m] ) 
			{
				oFe.setDate( d);
				sty =  ( oFe < oHoy )  ? cEA : ( oFe > oHoy ) ? cEM: cEH;
				tab.rows[i].cells[j].innerHTML = "<a style='" + sty + "' href='javascript://void(0);' onclick='" + fun+ " ( \"" + cFi + "\", \"" +d  +"/"+ m +"/"+ y + "\", \"" + cFH + "\" );' >" + d + "</a>";
			} else tab.rows[i].cells[j].innerHTML = '';
}
//////////////////////////////// Check ////////////////////////////////
function checkAll(  cFN, cFi, bValue )
{
	frm = id( cFN );
	for( var i = 0; i < frm.length; i++ )
		if ( frm[i].name == cFi + '[]')
			frm[i].checked = bValue;
}
function invertCheck(  cFN, cFi )
{
	frm = id( cFN );
	for( var i = 0; i < frm.length; i++ )
		if ( frm[i].name == cFi + '[]')
			frm[i].checked = !frm[i].checked;
}
//////////////////////////////// LookUp ////////////////////////////////
function refreshLookUp()
{
	setSending ( 0 );
	var cDiv = '';
	if (goXHR.readyState==4)
	{
		oDiv = id( "divLookUp_" + gLF );
		oDiv.style.visibility = "visible";
		htm = goXHR.responseText;
		var aPa = gLV.split(" ");
		if ( htm )
		{
			var aLineas = htm.split("\n");
			cDiv = "<table cellspacing='0' cellpadding='0' style='width: 100%'>";
			cDiv += "<tr><td style='text-align:right; border-bottom: 1px solid black;'><a href='Javascript:hideLookUp ( \"" + gLF +"\" );'>[x]</a> </td></tr>" ;
			for( i = 0; i < aLineas.length; i++)
			{
				var aDato = aLineas[i].split(";");
				if ( aDato[0] && aDato[1] )
				{
					cDe = " " + aDato[1];
					for( j = 0; j < aPa.length; j++)
					{
						nPos = cDe.toLowerCase().indexOf ( " " + aPa[j].toLowerCase() );
						if ( nPos > 0 ) cDe =  cDe.substr(0, nPos + 1) + cDe.substr(nPos + 1, aPa[j].length).bold() + cDe.substr(nPos + aPa[j].length + 1);
					}
					aDato[1].replace("'"," ");
					cDiv += "<tr><td style='cursor: pointer;' id='cellLookUp_" + gLF + i + "' onclick='selectLookUp( \"" + gLF + "\", \"" + aDato[0] + "\", \"" + aDato[1].replace(/^\s+/,"") + "\");'>" + cDe + "</td></tr>" ;
				}
			}
			cDiv += "</table>";
			oDiv.innerHTML  = cDiv;
			gLI = -1;
			gLM = aLineas.length - 1;
		} else
		{
			cDiv = "<table cellspacing='0' cellpadding='0' style='width: 100%'>";
			cDiv += "<tr><td style='text-align:right; border-bottom: 1px solid black;'><a href='Javascript:hideLookUp ( \"" + gLF +"\" );'>[x]</a> </td></tr>" ;
			cDiv += "<tr><td>No hay datos</td></tr>" ;
			cDiv += "</table>";
			oDiv.innerHTML  = cDiv;
			gLM = 0;
		}
		//gcElement = '';
	}
}
function selectLookUp( cFi, cVK, cVD )
{
	var oDiv = id( "divLookUp_" +  cFi );
	oDiv.style.visibility = "hidden";
	var oValue = id( cFi  + '_Key' );
	var oIn = id( cFi  + '_Desc' );
	oValue.value = cVK;
	oIn.value = cVD;
	if ( oValue.getAttribute('onchange') )
		oValue.onchange();
}
function blurLookUp ( cFi, cUrl, e , cLa, bOb )
{
	oIn = id( cFi  + '_Desc' );
	oValue = id( cFi  + '_Key' );
	if ( oIn.value.length == 0 ) oValue.value = 0;
	// Si esta en modo chequear el control, se fija si tiene datos
	if ( gCF  )
		if ( bOb == 1 && oValue.value.length == 0 )
			return  "[" + cLa + "] El campo es obligatorio.\n" ;
	return "";
}
function keydownLookUp ( cFi, cUrl, e , cFD)
{
	var oDiv = id( "divLookUp_" + cFi);
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	switch ( k )
	{
		case 27: // Esc
			var oDiv = id( "divLookUp_" +  cFi );
			oDiv.style.visibility = "hidden";
			break;
		case 13: // Enter
			oIn = id( cFD );

			//if ( gLM == 1 && bOb ) gLI  = 0;
			if ( gLI >= 0 )
			{
				oCell = id( "cellLookUp_" + cFi + gLI );
				oCell.onclick();
			}
			oDiv = id( "divLookUp_" + cFi );
			oDiv.style.visibility = "hidden";
			gLI = -1;
			gLM = 0;
			oIn.onblur();
			return 0;
			break;
		case 38: //Sube
			if ( gLM > 1 )
			{
				if ( gLI >= 0 )
				{
					oCell = id( "cellLookUp_" + cFi  + gLI );
					oCell.style.backgroundColor = "#dddbce";
				} else gLI = 0;
				if ( --gLI < 0 ) gLI = gLM-1;
				oCell = id( "cellLookUp_" + cFi  + gLI );
				oCell.style.backgroundColor = "#FFE6E6";
			}
			break;
		case 40: //Bajo
			if ( gLM >= 1 )
			{
				if ( gLI >= 0 )
				{
					oCell = id( "cellLookUp_" + cFi  + gLI );
					oCell.style.backgroundColor = "#dddbce";
				}
				if ( ++gLI >= gLM ) gLI = 0;
				var oCell = id( "cellLookUp_" + cFi  + gLI );
				oCell.style.backgroundColor = "#FFE6E6";
			}
			break;
		case 8: // Back Space
		case 46: // Del
			break;
	}
	return 1;
}
function keyPressLookUp ( e )
{
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	if ( k == 13 ) return 0;
	return 1;
}
function changeLookUp ( cFi, cUrl, e , cFV, cDF )
{
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	gLF = cFi;
	if ( k == 0 || k == 27 || k == 13 || k == 9 || k == 16 || k == 37 || k == 38 || k == 39 || k == 40 || e.altKey || e.ctrlKey ) return 0;
	var oIn = id( cFV  );
	// Posiciona el Div
	var oDiv = id( "divLookUp_" + cFi );
	oDiv.style.visibility = "hidden";
	var nLeftPos = oIn.offsetLeft;
	var nWidth = oIn.offsetLeft;
	var nTopPos = oIn.offsetTop ;
	oDiv.style.left = nLeftPos + "px";
	oDiv.style.top = (nTopPos + 22) + "px";
	// Toma el valor
	val = oIn.value;
	nPos = val.lastIndexOf(";");
	if ( nPos > 0 )  val = val.substring(nPos);
	/*
	if ( k >= 32 && !e.altKey && !e.ctrlKey )
		val = oIn.value + String.fromCharCode(k); // Para ver por que no es asi !!!!
	*/
	if ( val == '' ) // || (isNaN( val ) && val.length < 3)  )
		return 1;
	cUrl += '&kl=' + val;
	gLV = val;

	oAhora = new Date();
	gTO = oAhora.valueOf();
	setTimeout( cDF + "('" + cUrl + "' , '" + cFi + "')", 500);
	return 0;
}
function delaySendLoopkUP(cUrl, cFi)
{
	oAhora = new Date();
	nDif = oAhora .valueOf() - gTO;
	if ( nDif >= 300 )
		send ( cUrl, refreshLookUp, "divLookUp_" + cFi, 1, 0 );
}
function delaySendInputText(cUrl, cFi)
{
	oAhora = new Date();
	nDif = oAhora .valueOf() - gTO;
	if ( nDif >= 300 )
		send ( cUrl, refreshInputText, "divLookUp_" + cFi, 1, 0 );
}
function selectInputText( cFi, cVD )
{
	var oDiv = id( "divLookUp_" +  cFi );
	oDiv.style.visibility = "hidden";
	var oIn = id( cFi );
	val = oIn.value;
	nPos = val.lastIndexOf(";");
	oIn.value =  ( nPos > 0 )  ? val.substring(0, nPos + 1) + cVD : cVD;
	// Si existe dispara el evento onchange
	if ( oIn.getAttribute('onchange') )
		oIn.onchange();
	oIn.focus();
}
function refreshInputText()
{
	setSending ( 0 );
	var cDiv = '';
	if (goXHR.readyState==4)
	{
		oDiv = id( "divLookUp_" + gLF );
		oDiv.style.visibility = "visible";
		htm = goXHR.responseText;
		var aPa = gLV.split(" ");
		if ( htm )
		{
			var aLineas = htm.split("\n");
			cDiv = "<table cellspacing='0' cellpadding='0' style='width: 100%'>";
			cDiv += "<tr><td style='text-align:right; border-bottom: 1px solid black;'><a href='Javascript:hideLookUp(\"" + gLF +"\");'>[x]</a> </td></tr>" ;
			for( i = 0; i < aLineas.length; i++)
			{
				cDe = " " + aLineas[i];
				for(j = 0; j < aPa.length; j++)
				{
					nPos = cDe.toLowerCase().indexOf(" " + aPa[j].toLowerCase() );
					if ( nPos > 0 ) cDe =  cDe.substr(0, nPos + 1) + cDe.substr(nPos + 1, aPa[j].length).bold() + cDe.substr(nPos + aPa[j].length + 1);
				}
				aLineas[i].replace("'"," ");
				cDiv += "<tr><td style='cursor: pointer;' id='cellLookUp_" + gLF + i + "' onclick='selectInputText( \"" + gLF + "\",  \"" + aLineas[i].replace(/^\s+/,"") + "\");'>" + cDe + "</td></tr>" ;
			}
			cDiv += "</table>";
			oDiv.innerHTML  = cDiv;
			gLI = -1;
			gLM = aLineas.length - 1;
		} else
		{
			cDiv = "<table cellspacing='0' cellpadding='0' style='width: 100%'>";
			cDiv += "<tr><td style='text-align:right; border-bottom: 1px solid black;'><a href='Javascript:hideLookUp(\"" + gLF +"\" );'>[x]</a> </td></tr>" ;
			cDiv += "<tr><td>No hay datos</td></tr>" ;
			cDiv += "</table>";
			oDiv.innerHTML  = cDiv;
			gLM = 0;
		}
		//gcElement = '';
	}
}
function hideLookUp ( cFi )
{
	var oDiv = id( "divLookUp_" +  cFi );
	oDiv.style.visibility = "hidden";
}
//////////////////////////////// Form y validaciones ////////////////////////////////	
function checkForm( frm )
{
	var cMe = "";
	var oObjeto;
	cVa  ="";
	if ( frm.getAttribute( "error") == 1 )
	{
		alert("hay un error en el formulario, lea el panel de mensajes por favor");
		return 0;
	}
	gCF = 1;
	for( var i = 0; i < frm.length; i++ )
	{
		try {
			cVa = '';
			if ( frm[i].onblur )
				cVa = frm[i].onblur();
			if ( cVa && cVa != "" )
			{
				// Guarda una Referencia al Primer Objeto donde se produjo un Error (para hacer focus)
				if ( oObjeto == void 0 ) oObjeto = frm[i];

				cMe += cVa +  "\n";
			}
		} catch(ex) {
		
		}
	}
	gCF = 0;
	if (cMe != "" ) {
		oObjeto.focus();
		return 0;
	}
	return 1;
}
function checkText(oThis, cLa, bOb )
{
	cMe = "";
	// Si no esta seteado el flag de validacion => saltea (lo pone el checkForm)
	if ( gCF == 0 ) return 1;
	min      = oThis.getAttribute('nMinimo');
	nLV   = oThis.value.length;

	if ( bOb == 1 && nLV == 0 ) {
		oThis.style.border = '1px solid #E2453E';
		cMe = "[" + cLa + "] El campo es obligatorio.\n" ;
	} else oThis.style.border = '1px solid black';

	if ( min > 0 && nLV < min && nLV > 0 && cMe == "" )
		cMe = "[" + cLa + "] El campo debe tener como minimo " + min + " caracteres.\n"  ;
	return cMe;
}
function checkNumber(oThis, cLa, bOb )
{
	cMe = "";
	// Si no esta seteado el flag de validacion => saltea (lo pone el checkForm)
	if ( 	gCF == 0 ) return 1;
	if ( bOb == 1 && oThis.value.length == 0 ) {
		oThis.style.border = '1px solid #E2453E';
		cMe = "[" + cLa + "] El campo es obligatorio.\n" ;
	} else oThis.style.border = '1px solid black';

	if ( isNaN(oThis.value) ) 
		cMe = "[" + cLa + "] El campo debe de ser numerico.\n"  ;
	return cMe;
}
////////////////////////////////
// Funciones de Validacion Mientras esta Editando
////////////////////////////////
function checkEmailEdit( oThis,  cDomain, e)
{
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	if ( k == 64 )
	{
		oThis.value +=  '@' + cDomain;
		oThis.select(10, 15);
		return 0;
	}
	return 1;
}
function checkNumberEdit( oThis, max, e )
{
	bOk = 1;
	cDi = "1234567890";
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	// Para que deje movimiento dentro de la Edicion
	if ( k == 0 || k == 8 || k == 13 ) return 1;

	nPosMenos = oThis.value.indexOf("-" );
	if ( nPosMenos == -1 && String.fromCharCode(k) == '-' )
		oThis.value = "-" + oThis.value;
	n = oThis.value.indexOf(".");
	if ( max > 0 )
	{
		if ( n == -1)
		{
			cDi += ".";
		} else if ( oThis.value.length - n > max )
				bOk = 0;
	}
	if ( cDi.indexOf(String.fromCharCode(k) ) == -1 )
		bOk = 0;
	return bOk ;
}
function checkDateEdit( oThis, e )
{
	bOk = 1;
	cDi = "-/1234567890";
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	// Para que deje movimiento dentro de la Edicion
	if ( k == 0 || k == 8 ) return 1;

	if ( cDi.indexOf(String.fromCharCode(k) ) == -1 )
		bOk = 0;
	return bOk;
}
function checkTimeEdit( oThis, e )
{
	bOk = 1;
	cDi = "1234567890";
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	n = oThis.value.indexOf(":");
	if ( n == -1 )
		cDi += ":";
	// Para que deje movimiento dentro de la Edicion
	if ( k == 0 || k == 8 ) return 1;

	if ( cDi.indexOf(String.fromCharCode(k) ) == -1 )
		bOk = 0;
	return bOk ;
}
function checkTextEdit( oThis, nCase, e )
{
	var cChange ;
	var k = (isExplorer() ) ? window.event.keyCode : e.which;
	// Para que deje movimiento dentro de la Edicion
	if ( k == 0 || k == 8 ) return 1;
	if ( k == 39 )
		return 0;
	switch ( parseInt(nCase) )
	{
		case 0:
			cChange = String.fromCharCode(k).toLowerCase().charCodeAt();
			break;
		case 1:
			cChange = String.fromCharCode(k).toUpperCase().charCodeAt();
			break;
		case 2:
			break;
		default:
			cChange = 0;
			break;
	}
	if ( cChange && isExplorer() )
		window.event.keyCode = cChange;
	return 1;
}
////////////////////////////////
// Funciones para el control Ranking
////////////////////////////////
function rank_out(cFi, cTextOver )
{
	oValue = id( cFi );
	rank_value( cFi,  oValue.value  );
	rank_over(cFi, cTextOver );
}
function rank_over(cFi, cTextOver )
{
	oLa = id(cFi + '_rank_text');
	oLa.innerHTML = cTextOver;
}
function star_click( cFi, value )
{
	rank_value( cFi, value );
	oValue = id( cFi );
	oValue.value = value;
}
function star_over( cFi, value, cLa, oThis  )
{
	rank_value( cFi, value );
	oLa = id( cFi + '_rank_text');
	oLa.innerHTML = cLa;
}
function rank_value( cFi,  value )
{
	for( i = 1; i <= 5 ; i++ )
	{
		imagen = (i <= value) ? 'con_estrella.gif' : 'sin_estrella.gif';
		oRank = id( cFi + '_rank_star' + i);
		oRank.style.backgroundImage = 'url( ' + gRI + '/' + imagen + ')';
	}
}
////////////////////////////////
// Funciones para el InputPhoto
////////////////////////////////
// Variables del contexto del Control, se Inicializan con initPhoto (cuando se sube una foto)
gPH = gPW = gPL = gPT =  0 ;
// Variables internas, se cargan con los eventos del mousedown
gPX=gPY= gPFx = gPFy = gPSx= 0;
gPfM = gPfS = 0;

function initPhoto()
{
	oIn = id('width');
	gPW = oIn.value;
	oIn = id('height');
	gPH = oIn.value;
	oIn = id('offsetx');
	gPL = oIn.value;
	oIn = id('offsety');
	gPT = oIn.value;
	// Posiciona las imagenes centradas
	rescalePhoto(-1);
}
function image_down( e, oThis )
{
	gPfM = 1;
	oFondo = id('fondo_imagen');
	gPFx = oFondo.style.left.substring(0, oFondo.style.left.length-2);
	gPFy = oFondo.style.top.substring(0, oFondo.style.top.length-2);
	gPX = e.clientX;
	gPY = e.clientY;
	oThis.style.cursor = "move";
}
function image_up( e , oThis)
{
	gPfM = 0;
	gPY = gPX = 0;
	oThis.style.cursor = "default";
}
function image_out( e , oThis)
{
	if (!e) var e = window.event;
	if ( gPfM )
	{
		nLeft = oThis.style.left.substring(0, oThis.style.left.length-2);
		nWidth = oThis.style.width.substring(0, oThis.style.width.length-2);
		nDesde = parseInt(nLeft);
		nHasta = parseInt(nWidth) + nDesde;
		if (  e.layerX <= nDesde || e.layerX > nHasta )
			image_up( e , oThis);
		nTop = oThis.style.top.substring(0, oThis.style.top.length-2);
		nHeight = oThis.style.height.substring(0, oThis.style.height.length-2);
		nDesde = parseInt(nTop);
		nHasta = parseInt(nHeight) + nDesde;
		if (  e.layerY <= nDesde || e.layerY > nHasta )
			image_up( e , oThis);
	}
}
function image_move( e )
{
	if (!e) var e = window.event;
	if ( gPfM )
	{
		oFondo = id('fondo_imagen');
		oRecorte = id('recorte_imagen');
		oOffsetX = id('offsetx');
		oOffsetY = id('offsety');
		oFondo.style.left = (gPFx - (gPX - e.clientX)) + 'px';
		oFondo.style.top = (gPFy - (gPY - e.clientY)) + 'px';
		nOffsetX = (gPFx - (gPX - e.clientX)  - gPL );
		nOffsetY = (gPFy - (gPY - e.clientY) -  gPT);
		oRecorte.style.left =  nOffsetX + 'px' ;
		oRecorte.style.top = nOffsetY + 'px' ;
		oOffsetX.value = -nOffsetX ;
		oOffsetY.value = -nOffsetY ;
	}
}
function changeRelativeZoom( nOffset )
{
	oZoom = id('zoom');
	nNewZoom = parseInt(oZoom.value) + nOffset;
	if ( nNewZoom  < 1 || nNewZoom  > 100 ) return 0;
	changeZoom( nNewZoom );
}
function rescalePhoto( nRescale, nNewZoom , nZoom )
{
	oFondo = id('fondo_imagen');
	oRecorte = id('recorte_imagen');
	oControl = id('control');
	oOffsetX = id('offsetx');
	oOffsetY = id('offsety');
	// Busca el largo y ancho del control
	nControlWidth = oControl.style.width.substring(0, oControl.style.width.length-2);
	nControlHeight = oControl.style.height.substring(0, oControl.style.height.length-2);
	nCenterY = nControlHeight/2;
	nCenterX = nControlWidth/2;
	switch(nRescale)
	{
		case -1: // Center
			oImgFondo = id('fondo_img');
			if ( oImgFondo.width == 0 || oImgFondo.height == 0 )
			{
				nLeft = (( nControlWidth - gPW )/2) ;
				nTop = (( nControlHeight - gPH )/2);
			} else
			{
				nLeft = (( nControlWidth - oImgFondo.width )/2) ;
				nTop = (( nControlHeight - oImgFondo.height)/2);
			}
			break;
		case -2: 	// Re escala el left y el top de acuerdo al Centro
			nLeftActual = oFondo.style.left.substring(0, oFondo.style.left.length-2);
			nTopActual = oFondo.style.top.substring(0, oFondo.style.top.length-2);
			nLeft = nCenterX  -( nCenterX - nLeftActual) * ( nNewZoom / nZoom);
			nTop = nCenterY  -( nCenterY - nTopActual) * ( nNewZoom / nZoom);
			break;
		case -3: // Ppio Control
			nLeft = 0;
			nTop = 0;
			break;
	}
	nOffsetX = nLeft - gPL;
	nOffsetY = nTop -  gPT;
	oFondo.style.left = nLeft + 'px';
	oFondo.style.top = nTop + 'px';
	oRecorte.style.left =  nOffsetX + 'px' ;
	oRecorte.style.top = nOffsetY + 'px' ;
	oOffsetX.value = -nOffsetX ;
	oOffsetY.value = -nOffsetY ;
}
function changeZoom( nNewZoom )
{
	oZoom = id('zoom');
	nZoom = parseInt(oZoom.value) ;
	oFondo = id('fondo_imagen');
	oRecorte = id('recorte_imagen');
	bPredefinedZoom = ( nNewZoom  < 0) ? 1: 0;
	if ( bPredefinedZoom )
	{
		switch ( nNewZoom )
			{
				case -1: // Zoom 100
					nNewZoom = 100;
					break;
				case -2: // Zoom Control
				case -3: // Zoom Recorte
					oControl = ( nNewZoom  == -2 ) ? id('control') : id('recorte_imagen');
					nControlWidth = oControl.style.width.substring(0, oControl.style.width.length-2);
					nControlHeight = oControl.style.height.substring(0, oControl.style.height.length-2);
					nZoomW = (nControlWidth * 100) / gPW;
					nZoomH =  (nControlHeight * 100) / gPH;
					nNewZoom = Math.min(nZoomW,nZoomH);
					break;
			}
	}
	if ( nNewZoom  < 1 || nNewZoom  > 100 ) return 0;
	// Cambia la Imagen
		oImgFondo = id('fondo_img');
		oImgRecorte = id('recorte_img');
		coef = ( nNewZoom  ) / 100;
		nWidthNuevo =  (gPW * coef);
		oImgFondo.width= (gPW * coef) ;
		oImgRecorte.width = (gPW * coef);
		oZoom.value = nNewZoom ;
	// Re posiciona la imagen
		rescalePhoto(  (bPredefinedZoom ? -1: -2 ) , nNewZoom, nZoom );
}
//////////////////////////////// DoubleList ////////////////////////////////
function dblListMoveItem(cInput, cOrigen, cDestino, oItem, cSe )
{
	oOr = id( cOrigen);
	oDe = id( cDestino  );
	oHi = id( cInput );
	// Busca al Group en el destino
		grp = oItem.parentNode.label; oGr = null;
		for( i = 0; i < oDe.childNodes.length && oGr == undefined; i++ )
			if ( oDe.childNodes[i].label == grp )
				oGr = oDe.childNodes[i];
	// Si no encuentra el Group en el Destino => lo agrega
		if ( oGr == undefined )
		{
			oGr = document.createElement('optgroup');
			oGr.label = grp;
			oDe.appendChild(oGr);
		}
	// El item nuevo lo agrega al destino
		opt = new Option( oItem.text, oItem.value);
		try {
			oDe.add(opt, null ); // standards compliant; doesn't work in IE
		} catch(ex) {
			oDe.add(opt, oDe.selectedIndex); // IE only
		}
		oGr.appendChild(opt);
	// Le pone el evento al item
		opt.setAttribute( "ondblclick", "dblListMoveItem( '" + cInput + "', '" + cDestino + "', '" + cOrigen + "', this, '" + cSe + "' )" );
	// Elimna el item del origen y si no tiene mas items el group => tambien lo elimina
		oGP = oItem.parentNode;
		oGP.removeChild(oItem);
		if ( oGP.childNodes.length == 0 )
			oOr.removeChild( oGP );
	// El value lo agrega o quita de input que sale en
		if ( cInput + '_list' == cOrigen )
		{
			if ( oHi.value != "" ) oHi.value += cSe ;
			oHi.value += oItem.value ;
		} else
		{
			if ( oOr.length >= 1 )
			{
				val = oItem.value;
				cHiddenValue = cSe +  oHi.value + cSe;
				from = cHiddenValue.indexOf( cSe + val + cSe);
				if ( from == undefined ) return 0;
				to = from + cSe.length + val.length;
				oHi.value = ( from == 0 ? cHiddenValue.substring(cSe.length + to, cHiddenValue.length -cSe.length ) : cHiddenValue.substring(cSe.length, from ) + cHiddenValue.substring(to, cHiddenValue.length -cSe.length ))  ;
			} else oHi.value = "";
		}
		if ( oHi.getAttribute('onchange') )
			oHi.onchange();
}
function dblListSelect(cInput, cOrigen, cDestino, bAll, cSe )
{
	oOr = id( cOrigen);
	for( i = 0; i < oOr.length; )
		if ( bAll || oOr[i].selected  )
		{
			dblListMoveItem(cInput, cOrigen, cDestino, oOr[i], cSe );
		} else i++;
}
//////////////////////////////// Multiple ////////////////////////////////	
function insertMultipleLookUp( cFN, cFi, cSe )
{
	oCbo  = eval("document." + cFN + "." + cFi + "_cbo");
	oFK  = eval("document." + cFN + "." + cFi + "_txt_Key");
	oFD  = eval("document." + cFN + "." + cFi + "_txt_Desc");
	oHi  = eval("document." + cFN + "." + cFi + "_val");
	oLst  = eval("document." + cFN + "." + cFi + "_lst");
	if ( oFK.value )
	{
		oCbo.value = 1;
		// Lo agrega al Input Oculto
		if ( oLst.length >= 1 ) oHi.value += cSe ;
		oHi.value += oFK.value ;
		// Lo agrega a la lista
		opt = new Option( oFD.value, oFK.value);
		oFK.value = "";
		oFD.value = "";
		try {
			oLst.add(opt, null ); // standards compliant; doesn't work in IE
		} catch(ex) {
			oLst.add(opt, oLst.selectedIndex); // IE only
		}
	}
}
function insertMultipleArray( cFN, cFi, cSe, cSeValue )
{
	oCbo  = eval("document." + cFN + "." + cFi + "_cbo");
	oHi  = eval("document." + cFN + "." + cFi + "_val");
	oLst  = eval("document." + cFN + "." + cFi + "_lst");
	oDivArray  = id( cFi + "_txt" );
	if ( oDivArray != undefined )
	{
		val  = '' ;
		des = '' ;
		nqChilds = oDivArray.childNodes.length;
		for( i = 0 ; i < nqChilds; i++ )
		{
			if (oDivArray.childNodes[i].nodeType == 1 && oDivArray.childNodes[i].style.visibility != 'hidden' )
			{
				if ( oDivArray.childNodes[i].nodeName == 'SELECT' )
				{
					if ( oDivArray.childNodes[i].length > 1 && oDivArray.childNodes[i].selectedIndex )
					{
						val += oDivArray.childNodes[i].options[oDivArray.childNodes[i].selectedIndex].value ;
						des  += oDivArray.childNodes[i].options[oDivArray.childNodes[i].selectedIndex].text + ' ' ;
					} else
					{
						des += ' ' ;
					}
				}else // INPUT
				{
					val += oDivArray.childNodes[i].value ;
					des += oDivArray.childNodes[i].value + ' ' ;
				}
				// Si no es el ultimo => concatena con el separador
				if ( i < (nqChilds-1) ) val  += cSeValue;
			}
		}
		oCbo.value = 1;
		// Lo agrega al Input Oculto
		if ( oLst.length >= 1 ) oHi.value += cSe ;
		oHi.value += val ;
		// Lo agrega a la lista
		opt = new Option( des , val);
		try {
			oLst.add(opt, null ); // standards compliant; doesn't work in IE
		} catch(ex) {
			oLst.add(opt, oLst.selectedIndex); // IE only
		}
	}
}
function insertMultiple( cFN, cFi, cSe )
{
	oCbo  = eval("document." + cFN + "." + cFi + "_cbo");
	oFi  = eval("document." + cFN + "." + cFi + "_txt");
	oHi  = eval("document." + cFN + "." + cFi + "_val"); // Es un campo oculto que tiene el valor que envia el formulario (seria como un espejo de los valores del select)
	oLst  = eval("document." + cFN + "." + cFi + "_lst"); //Es el select que muestra los n registros (el submit no lo envia)
	if ( oFi.value )
	{
		oCbo.value = 1;
		// Lo agrega al Input Oculto
		if ( oLst.length >= 1 ) oHi.value += cSe ;
		oHi.value += oFi.value ;
		// Lo agrega a la lista
		opt = new Option( oFi.value, oFi.value);
		oFi.value = "";
		try {
			oLst.add(opt, null ); // standards compliant; doesn't work in IE
		} catch(ex) {
			oLst.add(opt, oLst.selectedIndex); // IE only
		}
	}
}
function removeMultiple( cFN, cFi, cSe )
{
	oCbo  = eval("document." + cFN + "." + cFi + "_cbo");
	oHi  = eval("document." + cFN + "." + cFi + "_val");
	oLst  = eval("document." + cFN + "." + cFi + "_lst");
	// Si hay un option seleccionado en la lista
	if ( oLst.selectedIndex >= 0)
	{
		oCbo.value = 1;
		// Quita el elemento seleccionado del Input Oculto
		if ( oLst.length > 1 )
		{
			val = oLst.options[oLst.selectedIndex].value;
			cHiddenValue = cSe +  oHi.value + cSe;
			from = cHiddenValue.indexOf( cSe + val + cSe);
			if ( from == undefined ) return 0;
			to = from + cSe.length + val.length;
			oHi.value = ( from == 0 ? cHiddenValue.substring(cSe.length + to, cHiddenValue.length -cSe.length ) : cHiddenValue.substring(cSe.length, from ) + cHiddenValue.substring(to, cHiddenValue.length -cSe.length ))  ;
		} else oHi.value = "";
		// Quita el elemento seleccionado de lista
		oLst.remove( oLst.selectedIndex );
	}
	return 1;
}
//////////////////////////////// Set ////////////////////////////////	
function addSet( cFi )
{
	oIn = id( cFi + '_Input'   );
	oSet = id( cFi + '_Set'   );
	// Verifica si Existe
	for( i = 0 ; i < oSet.length; i++ )
	{
		if ( oSet.options[i].value == oIn.value )
		{
			alert ( "El valor ya esta ingresado" ) ;
			return 0;
		}
	}
	opt = new Option( oIn.value, oIn.value );
	oIn.value = "";
	try {
		//oSet.options[oSet.selectedIndex] = opt;
		oSet.add(opt, null ); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		oSet.add(opt, oSet.selectedIndex); // IE only
	}
	return 1;
}
function removeSet( cFi )
{
	var oSet = id( cFi + '_Set'   );
	oSet.remove( oSet.selectedIndex);
	return 1;
}